package org.dousb.doupicturebackend.manager.websocket;


import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * WebSocketConfig
 * 定义连接
 *
 * @author 豆沙包
 * @version 1.0
 * @since 2025-11-06  16:22
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Resource
    private PictureEditHandler pictureEditHandler;

    @Resource
    private WSHandshakeInterceptor wsHandshakeInterceptor;

    /**
     * 注册WebSocket处理器的方法
     *
     * @param registry WebSocket处理器注册器，用于注册WebSocket处理器和拦截器
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册pictureEditHandler处理器，并指定WebSocket的访问路径为"/ws/picture/edit"
        // 同时添加wsHandshakeInterceptor握手拦截器
        registry.addHandler(pictureEditHandler, "/ws/picture/edit")
                .addInterceptors(wsHandshakeInterceptor)
                .setAllowedOrigins("*");
    }
}